﻿using System;
using System.Collections.Generic;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Windows.Forms;
using System.IO;

using System.Data.SqlClient;
using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using System.Xml;

using Common;


namespace CNCSoft.ErpApps.TMS
{
    class PublicFunction
    {

        public static void SetDBLogonForReport(ReportDocument cryRpt)
        {
            string strPass = "";
            string strCN = GlobalVariable.WSInfo.ConnectionString.ToLower();
            strCN = strCN.Substring(strCN.IndexOf("password"));
            strPass = strCN.Substring(strCN.IndexOf("=") + 1).Trim();

            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            
            crConnectionInfo.AllowCustomConnection = true;
            crConnectionInfo.IntegratedSecurity = false;
            crConnectionInfo.Type = ConnectionInfoType.SQL;
            crConnectionInfo.ServerName = GlobalVariable.WSInfo.SqlServerName;
            crConnectionInfo.DatabaseName = GlobalVariable.WSInfo.SqlServerDatabaseName;
            crConnectionInfo.UserID = GlobalVariable.WSInfo.SqlServerUserName;
            crConnectionInfo.Password = strPass;
            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            
        }

        public static void SetParameter(ReportDocument cryRpt, string strParaName, object ParaValue)
        {
            try
            {
                ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = ParaValue;
                crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[strParaName];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
            }
            catch (Exception ex)
            {
                //Cứ việc truyên tham số: thiếu thì không hiện đúng báo cáo, dư thì thôi. Không báo lỗi trong tất cả các TH
                //MessageBox.Show(ex.Message, "Set parameter: " + strParaName + " error!");
            }

        }

        private void SetDBLogonForReport_old(CrystalDecisions.Windows.Forms.CrystalReportViewer CRViewer)
        {
            string strPass = "";
            string strCN = GlobalVariable.WSInfo.ConnectionString.ToLower();
            strCN = strCN.Substring(strCN.IndexOf("password"));
            strPass = strCN.Substring(strCN.IndexOf("=") + 1).Trim();

            ConnectionInfo connectionInfo = new ConnectionInfo();
            connectionInfo.ServerName = GlobalVariable.WSInfo.SqlServerName;
            connectionInfo.DatabaseName = GlobalVariable.WSInfo.SqlServerDatabaseName;
            connectionInfo.UserID = GlobalVariable.WSInfo.SqlServerUserName;
            connectionInfo.Password = strPass;

            TableLogOnInfos tableLogOnInfos = CRViewer.LogOnInfo;

            foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
            {
                tableLogOnInfo.ConnectionInfo = connectionInfo;
            }

        }


        //Phan quyen THEM, SUA, XOA tren form chi tung User tuong ung
        public static void PhanQuyenForm(string strFormName, string strUserID, out bool bAddnew, out bool bEdit, out bool bDelete)
        {
            //Neu khong co trong database (tale: SYS_PhanQuyenForm) thi xem nhu co toan quyen
            bool bThem = true;
            bool bSua = true;
            bool bXoa = true;
            SqlDataReader PQ_DR = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString, "SELECT [Addnew], [Edit], [Delete] FROM SYS_PhanQuyenForm " +
                                  "WHERE FormName = '" + strFormName + "' AND UserID = '" + strUserID + "'");
            while (PQ_DR.Read())
            {
                try
                {
                    bThem = (bool)PQ_DR.GetValue(0);
                    bSua = (bool)PQ_DR.GetValue(1);
                    bXoa = (bool)PQ_DR.GetValue(2);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            bAddnew = bThem;
            bEdit = bSua;
            bDelete = bXoa;
        }


       public static string ReadFileText(string strFileName, int intDong)
       {
            string strReturn = "";
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(strFileName)) 
                {
                    //String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.

                    for (int i = 1; i <= intDong; i++)
                        strReturn = sr.ReadLine();                   
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                MessageBox.Show(e.Message);
            }
           return strReturn;
       }


       public static void WriteFileText(string strFileName, int intDong, string strWrite)
       {
           try
           {
               // Create an instance of StreamReader to read from a file.
               // The using statement also closes the StreamReader.
               using (StreamWriter sw = new StreamWriter(strFileName))
               {
                   for (int i = 1; i <= intDong; i++)
                   {
                       sw.WriteLine();
                       if (i == intDong)
                           sw.WriteLine(strWrite);
                   }
               }
           }
           catch (Exception e)
           {
               // Let the user know what went wrong.
               MessageBox.Show(e.Message);
           }
       }

       public static void WriteXMLFile(string XMLFileName, string strElement, string strContent )
       {
           Stream xmlFile = new FileStream(XMLFileName, FileMode.Append);
           XmlTextWriter textWritter = new XmlTextWriter(xmlFile, Encoding.Default);
           textWritter.WriteStartDocument();
           textWritter.WriteStartElement("INIT");

           //Back ground
           textWritter.WriteStartElement(strElement, "");
           textWritter.WriteString(strContent);
           textWritter.WriteEndElement();

           //textWritter.WriteEndElement();  //INIT

           textWritter.WriteEndDocument();
           textWritter.Close();
       }

       
    }
}
